//
//  MoreAnchorController.m
//  瓜子电台
//
//  Created by 田阳 on 16/8/11.
//  Copyright © 2016年 YueZhang. All rights reserved.
//

#import "MoreAnchorController.h"
#import "AnchorListModel.h"
#import "CategorySecondCell.h"
@interface MoreAnchorController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

//pageID
@property(nonatomic,assign) NSInteger pageID;
//存放model数组
@property(nonatomic,strong) NSMutableArray *modelArray;
//最大刷新数
@property(nonatomic,assign) NSInteger mostPage;
@end

@implementation MoreAnchorController

- (NSMutableArray *)modelArray
{
    if (!_modelArray) {
        self.modelArray = [[NSMutableArray alloc] init];
    }
    return _modelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self requestData];
    [self creatSegmentView];
    [self creatCollectionView];
    [self refreshData];
}

- (void)refreshData
{

    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.modelArray removeAllObjects];
        [self requestData];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageID++;
        [self requestData];
    }];
}

- (void)creatSegmentView
{
    self.segmentView = [[UISegmentedControl alloc] initWithItems:@[@"最热",@"最新"]];
    _segmentView.frame = CGRectMake(10 , 5, kScreenWidth - 20, 30);
    _segmentView.backgroundColor = [UIColor whiteColor];
    _segmentView.selectedSegmentIndex = 0;
    _segmentView.tintColor = [UIColor redColor];
    _segmentView.layer.borderWidth = 0.5f;
    _segmentView.layer.borderColor = [[UIColor redColor]CGColor];
    [self.view addSubview:_segmentView];
}

- (void)creatCollectionView
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    _flowLayout.itemSize = CGSizeMake(kScreenWidth, 90);
    _flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _flowLayout.minimumLineSpacing = 0;
    _flowLayout.minimumInteritemSpacing = 0;
    
   if (_type == 0) {
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.segmentView.endY, kScreenWidth, kScreenHeight - 144) collectionViewLayout:_flowLayout];
    }else{
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 114) collectionViewLayout:_flowLayout];
        self.segmentView.hidden = YES;
    }

    [_collectionView registerNib:[UINib nibWithNibName:@"CategorySecondCell" bundle:nil] forCellWithReuseIdentifier:@"secondCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
}

- (void)requestData
{
    if (_type == 0) {
        NSString *str = [NSString stringWithFormat:MoreAnchorURL5,self.myIndex,_pageID];
        [self dataRequestWithUrl:str];
    }else{
        NSString *str = [NSString stringWithFormat:MoreAnchorURL,self.myIndex,_pageID];
        [self dataRequestWithUrl:str];
    }
}

- (void)dataRequestWithUrl:(NSString *)str
{
    if (_pageID == 0) {
        self.pageID = 1;
    }

    [NetWorkrequestManage requestWithType:GET url:str parameters:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        self.mostPage = [dic[@"maxPageId"] integerValue];
        NSLog(@"最大的%ld",self.mostPage);
        NSArray *array = dic[@"list"];
        for (NSDictionary *dict in array) {
            AnchorListModel *model = [[AnchorListModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            [self.collectionView.mj_header endRefreshing];
            [self.collectionView.mj_footer endRefreshing];
            if (self.pageID == self.mostPage) {
                [self.collectionView.mj_footer endRefreshingWithNoMoreData];
            }
        });
    } error:nil];
}

#pragma mark ----collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategorySecondCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"secondCell" forIndexPath:indexPath];
    AnchorListModel *model = self.modelArray[indexPath.row];
    [cell setDataWithModel:model];
    return cell;
}
@end
